package cn.exrick.xboot.modules.commonlyUsed.controller;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.commonlyUsed.dto.CommonlyDTO;
import cn.exrick.xboot.modules.commonlyUsed.dto.CommonlyVO;
import cn.exrick.xboot.modules.commonlyUsed.entity.CommonlyUsed;
import cn.exrick.xboot.modules.commonlyUsed.entity.OrgCommonly;
import cn.exrick.xboot.modules.commonlyUsed.service.IOrgCommonlyService;
import cn.hutool.core.lang.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(description = "组织机构常用功能管理")
@RequestMapping("/xboot/orgCommonly")
@Transactional
public class OrgCommonlyController{

    @Autowired
    IOrgCommonlyService orgCommonlyService;

    @RequestMapping(value = "/getByOrg", method = RequestMethod.GET)
    @ApiOperation(value = "根据组织机构获取不同的常用功能")
    public Result<List<CommonlyVO>> getAll(@ModelAttribute CommonlyDTO commonlyDTO){
        List<CommonlyVO> list = orgCommonlyService.orgCommonlyList(commonlyDTO);
        return new ResultUtil<List<CommonlyVO>>().setData(list);
    }

    @RequestMapping(value = "/getOpenAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取常用功能注册的全部数据")
    public Result<List<CommonlyVO>> getOpenAll(@ModelAttribute CommonlyDTO commonlyDTO){

        List<CommonlyVO> list = orgCommonlyService.openList(commonlyDTO);
        return new ResultUtil<List<CommonlyVO>>().setData(list);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "对组织机构插入常用功能")
    public Result<OrgCommonly> saveOrUpdate(@RequestBody CommonlyDTO commonlyDTO){

        if(orgCommonlyService.InsertOrUpdateCommonly(commonlyDTO)){
            return new ResultUtil<OrgCommonly>().setSuccessMsg("操作成功");
        }
        return new ResultUtil<OrgCommonly>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除常用功能注册")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            orgCommonlyService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}

